//
//  YWFeedBackViewController.m
//  maintain
//
//  Created by Kyo on 15/11/3.
//  Copyright © 2015年 ewin. All rights reserved.
//

#import "YWFeedBackViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "MBProgressHUD.h"
#import "YWConst.h"

@interface YWFeedBackViewController ()<UITextViewDelegate>{
    UITextView *feedBackTextView;
    UILabel *placeHolderLabel;
}
@property (nonatomic, weak) MBProgressHUD* HUD;
@end

@implementation YWFeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"feedBack", nil);
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor customBackgroudColor];
    [self addTextView];
    [self initRightBarButtonItem];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self showkeyboard];
    });
}

- (MBProgressHUD*)HUD
{
    if (!_HUD) {
        MBProgressHUD* hud = [[MBProgressHUD alloc] initWithView:self.view];
        hud.labelFont = kHUDLabelFont;
        hud.labelText = NSLocalizedString(kTextBeingProcessed, nil);
        hud.dimBackground = YES;
        // hud.animationType = MBProgressHUDAnimationZoom;
        _HUD = hud;
        _HUD.tag = kMBProgressHUDTag;
        [self.view addSubview:hud];
    }
    return _HUD;
}

-(void)showkeyboard{
    [feedBackTextView becomeFirstResponder];
}

-(void)addTextView{
    UITextView *feedBackTV = [[UITextView alloc]initWithFrame:CGRectMake(kMarginW,15, SCREEN_WIDTH-kMarginW*2, 100)];
    feedBackTV.layer.borderColor = [UIColor customLineColor].CGColor;
    feedBackTV.layer.borderWidth = 1.0;
    feedBackTV.layer.masksToBounds = YES;
    feedBackTV.layer.cornerRadius = 4.0;
    
    feedBackTV.delegate = self;
    feedBackTV.font = kContentFont;
    feedBackTextView = feedBackTV;
    [self.view addSubview: feedBackTV];
    
    UILabel *placeHolderL = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, feedBackTV.frame.size.width - 20, kLabelH)];
    placeHolderL.font = kContentFont;
    placeHolderL.textColor = [UIColor lightGrayColor];
    placeHolderL.text = NSLocalizedString(@"feedBackPlaceHolder", nil);
    placeHolderLabel = placeHolderL;
    [feedBackTextView addSubview:placeHolderLabel];
}
- (void)initRightBarButtonItem
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"commit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(handlePostFeedBackEvent)];
    self.navigationItem.rightBarButtonItem.enabled = NO;
}
#pragma mark - textViewDelegate
- (void)textViewDidChange:(UITextView*)textView
{
    UITextRange *selectedRange = [textView markedTextRange];
    NSString * newText = [textView textInRange:selectedRange];
    if (textView.text.length - newText.length <= 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
    else {
        self.navigationItem.rightBarButtonItem.enabled = YES;
    }
    if (textView.text.length>0) {
        placeHolderLabel.hidden = YES;
    }else{
        placeHolderLabel.hidden = NO;
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)handlePostFeedBackEvent{
    NSString *feedBack = [feedBackTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    [feedBackTextView resignFirstResponder];
    if (feedBack.length == 0) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextFeedBackNotNull, nil)];
        return;
    }
    if ([YWCommonTool isContainsEmoji:feedBack]) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextCantContainEmoji, nil)];
        return;
    }
    if (feedBack.length > 200) {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"noteLengthMustBeLessThan200", nil)];
        return;
    }
    //提交反馈信息
    if ([[WMHttpTool shareHttpTool] existenceNetwork]) {
        [self.HUD show:YES];
        [self postFeedBackInfo:feedBack];
    }
    else {
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
    }
}

-(void)postFeedBackInfo:(NSString*)feedBack{
    NSDictionary *dic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = dic;
    if (dic == nil) {return;}
    
    NSDictionary* params = @{ @"content" : feedBack};
    YWWeakSelf(weakSelf);
    NSString *url = [YWCommonTool getFeedBackUrl];
    YWLog(@"反馈url:%@,params:%@",url,params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        [weakSelf.HUD hide:YES];
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextCommitSuccess, nil)];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        [weakSelf.HUD hide:YES];
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextModifyFeedBackFail, nil)];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
